package com.lin.cloud.disk.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.lin.cloud.disk.common.enums.EDeleteStatus;
import com.lin.cloud.disk.common.exception.BusinessException;
import com.lin.cloud.disk.entity.dto.LogicDeleteStatusDTO;
import com.lin.cloud.disk.entity.pojo.CompanyDepartment;
import com.lin.cloud.disk.entity.request.department.DeleteDepartmentRequest;
import com.lin.cloud.disk.entity.request.department.UpdateDepartmentRequest;
import com.lin.cloud.disk.mapper.CompanyDepartmentMapper;
import com.lin.cloud.disk.service.ICompanyDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 公司部门服务实现类
 * @author ljc
 * @date 2021/1/13 18:26
 */
@Service
@Slf4j
public class CompanyDepartmentServiceImpl implements ICompanyDepartmentService {

    @Resource
    private CompanyDepartmentMapper companyDepartmentMapper;

    /**
     * 获取部门ID列表 通过公司
     * @param companyId
     * @return
     */
    @Override
    public List<Long> getDepartmentIdListByCompany(Long companyId){
        List<Long> departmentIdList = companyDepartmentMapper.getDepartmentIdListByCompany(companyId);
        return CollectionUtils.isNotEmpty(departmentIdList) ? departmentIdList : Collections.EMPTY_LIST;
    }


    /**
     *  创建公司部门关系映射
     * @param companyDepartment
     */
    @Override
    public void createCompanyDepartment(CompanyDepartment companyDepartment){
        if (null == companyDepartment) {
            throw new BusinessException("新建公司部门关系记录失败");
        }
        long id = IdWorker.getId();
        companyDepartment.setId(id);
        companyDepartment.setCreateTime(new Date());
        companyDepartment.setIsDelete(EDeleteStatus.UN_DELETE.getStatus());
        companyDepartmentMapper.insert(companyDepartment);
    }

    /**
     * 删除公司部门
     * @param request
     */
    @Override
    public void deleteCompanyDepartment(DeleteDepartmentRequest request){
        LogicDeleteStatusDTO deleteStatusDTO = LogicDeleteStatusDTO.getDeleteStatusInstance(request.getDepartmentIdList(),request.getUserDTO().getUsername());
        Integer updateCount = companyDepartmentMapper.logicDeleteOrRecover(deleteStatusDTO);
        if (0 == updateCount){
            throw new BusinessException("删除公司部门失败,请公司部门是否存在");
        }
        return;
    }

    /**
     * 更新公司部门关联表信息
     * @param request
     */
    @Override
    public void updateCompanyDepartment(UpdateDepartmentRequest request){
        CompanyDepartment companyDepartment = new CompanyDepartment();
        companyDepartment.setCompanyId(request.getUserDTO().getCompanyId());
        companyDepartment.setDepartmentId(request.getDepartmentId());
        companyDepartment.setUpdateTime(new Date());
        companyDepartment.setUpdateBy(request.getUserDTO().getUsername());
        companyDepartmentMapper.updateCompanyDepartment(companyDepartment);
    }

    /**
     * 公司是否存在该部门
     * @param companyId
     * @param departmentId
     * @return
     */
    @Override
    public Boolean companyIsExistDepartment(Long companyId, Long departmentId){
        // 通过公司ID与部门Id查询
        CompanyDepartment companyDepartment = companyDepartmentMapper.selectByCompanyIdAndDepartmentId(companyId, departmentId);
        return  null == companyDepartment ? false :true;
    }
}
